<?php namespace system\model;

use hdphp\Model\Model;
class Shop extends Model{

	//数据表
	protected $table = "vivo_shop";

	//完整表名
	protected $full = false;

	//自动验证
	protected $validate=[
		//['字段名','验证方法','提示信息',验证条件,验证时间]
		['sname','required','请输出类型名称',3,3],
		['no','required','请输出货号',3,3],
		['marketprice','required','请输出市场价',3,3],
		['sunit','required','请输入单位',3,3],
		['commodityprice','required','请输入商品价',3,3],
		['stocks','required','请输入总库存',3,3],
		['picture','required','请输出列表图',3,3],
		['click','required','请输出点击次数',3,3],
		['service','required','请输出售后服务',3,3],
		['intro','required','请输出商品详情',3,3],
	];
	//自动完成
	protected $auto=[
		//['字段名','处理方法','方法类型',验证条件,验证时机]

	];

	//自动过滤
    protected $filter=[
        //[表单字段名,过滤条件,处理时间]
    ];

	//时间操作,需要表中存在created_at,updated_at字段
	protected $timestamps=false;


	//禁止插入的字段
	protected $denyInsertFields = [];

	//禁止更新的字段
	protected $denyUpdateFields = [];

	//前置方法 比如: _before_add 为添加前执行的方法
	protected function _before_add(){}
	protected function _before_delete(){}
	protected function _before_save(&$data){}

	protected function _after_add(){}
	protected function _after_delete(){}
	protected function _after_save(){}
	public function getAll(){

		$page = Page::row(10)->make($this->count());
		$data = $this->limit(Page::limit())->orderBy('sid','desc')->get();
		return ['page'=>$page,'data'=>$data];
	}
	public function store(){
		//验证
		if(!$this->create()) return false;
		//商品详情表的验证
		$shopDetailModel=new \system\model\Details();
		if (!$shopDetailModel->create()){
			//获取详情表的错误给$this->error
			$this->error = $shopDetailModel->getError();
			return false;
		}
		//商品属性表
		$shopAttrModel = new \system\model\Shopattr();
		//商品属性表的验证判断
		if (!$shopAttrModel->create()){
			//获取详情表的错误给$this->error
			$this->error = $shopDetailModel->getError();
			return false;
		}
		$this->data['shelftime'] = date('Y-m-d H:i:s',time());
		$this->data['aid']=$_SESSION['admin']['uid'];
		$sid = $this->add();
		//$_POST['logo']是多图上传提交过来的数据
		//执行商品详情表的添加
		$small = "";$medium="";$big="";
		//大中小图
		foreach ($_POST['logo'] as $k=>$v){
			$small .=Image::thumb($v,dirname($v).'/small_' . basename($v),50,50,5).",";
			$medium .=Image::thumb($v,dirname($v).'/medium_' . basename($v),80,80,5).",";
			$big .=Image::thumb($v,dirname($v).'/big_' . basename($v),150,150,5).",";
		}
		$small = rtrim($small,',');
		$big = rtrim($big,',');
		$medium = rtrim($medium,',');
		$data = [
			'small'=>$small,
			'medium'=>$medium,
			'big'=>$big,
			'intro'=>$_POST['intro'],
			'service'=>$_POST['service'],
			'sid'=>$sid,
		];
		$shopDetailModel->add($data);
		//执行商品属性表的添加
		//添加属性
		foreach ($_POST['attr'] as $k=>$v){
			if ($v){
				$data = [
					'stvalue'=>$v,
					'added'=>0,
					'taid'=>$k,
					'sid'=>$sid,
				];
				$shopAttrModel->add($data);
			}
		}
		//添加商品规格
		foreach ($_POST['spec'] as $k=>$v){
			if($v){

				foreach ($v['color'] as $key=>$value){
					$data = [
						'stvalue'=>$value,
						'added'=>$v['added'][$key],
						'taid'=>$k,
						'sid'=>$sid,
					];

					$shopAttrModel->add($data);
				}
			}

		}
		return true;
	}

	/**
	 * 商品编辑
	 */
	public function edit(){
		if (!$this->create()) return false;
		$detailModel = new \system\model\Details();
		if(!$detailModel->create()){
			$this->error = $detailModel->getError();
			return false;
		}
		//商品修改
		$this->save();
		//商品详情表修改
		$logo = $_POST['logo'];
		foreach ($logo as $k=>$v){
			if (substr(basename($v),0,3)=='big'){
				$logo[$k] = dirname($v) . '/' . ltrim(basename($v),'big_');
			}
		}
		foreach ($logo as $k=>$v){
			if (substr(basename($v),0,6)=='medium'){
				$logo[$k] = dirname($v) . '/' . ltrim(basename($v),'medium_');
			}
		}
		foreach ($logo as $k=>$v){
			if (substr(basename($v),0,5)=='small'){
				$logo[$k] = dirname($v) . '/' . ltrim(basename($v),'small_');
			}
		}
		$small = "";$medium="";$big="";
		//大中小图
		foreach ($logo as $k=>$v){
			$small .=Image::thumb($v,dirname($v).'/small_' . basename($v),50,50,5).",";
			$medium .=Image::thumb($v,dirname($v).'/medium_' . basename($v),80,80,5).",";
			$big .=Image::thumb($v,dirname($v).'/big_' . basename($v),150,150,5).",";
		}
		$small = rtrim($small,',');
		$big = rtrim($big,',');
		$medium = rtrim($medium,',');
		$data = [
			'small'=>$small,
			'medium'=>$medium,
			'big'=>$big,
			'intro'=>$_POST['intro'],
			'service'=>$_POST['service'],
			'sid'=>$_POST['sid'],
		];
		//获取商品详情表旧数据
		$detailData = $detailModel->where('sid',$_POST['sid'])->first();
		//把大图字符串转化为数组
		$detailData['big'] = explode(',',$detailData['big'] );
		$detailData['medium'] = explode(',',$detailData['medium'] );
		$detailData['small'] = explode(',',$detailData['small'] );

//		p($_POST['logo']);p($detailData['big']);die;
		foreach ($detailData['big'] as $k=>$v){
			if (file_exists($v) && !in_array($v,$_POST['logo'])){
				unlink($v);
			}
		}
		foreach ($detailData['medium'] as $k=>$v){
			if (file_exists($v) && !in_array($v,$_POST['logo'])){
				unlink($v);
			}
		}
		foreach ($detailData['small'] as $k=>$v){
			if (file_exists($v) && !in_array($v,$_POST['logo'])){
				unlink($v);
			}
		}
		$detailModel->where('sid',$_POST['sid'])->update($data);
		return true;
	}
}